package com.youmingxing.bean;

import android.content.Context;
import android.content.Intent;

import com.youmingxing.activity.AuditionDetailActivity;
import com.youmingxing.activity.AuditionSelectActivity;
import com.youmingxing.activity.ContentDetailActivity;
import com.youmingxing.activity.MainActivity;
import com.youmingxing.activity.MyNewsActivity;
import com.youmingxing.activity.WebViewActivity;
import com.youmingxing.activity.selfInfo.CertificationingActivity;
import com.youmingxing.activity.selfInfo.FansActivity;
import com.youmingxing.activity.selfInfo.MyAllWorksActivity;
import com.youmingxing.activity.selfInfo.MyAuditionGoingActivity;
import com.youmingxing.activity.selfInfo.PersonalInfoActivity;
import com.youmingxing.data.GlobalDataManager;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * @author evilyin(ChenZhixi)
 * @since 15/11/23
 */
public class Action {
    private int type = -1;
    private boolean isPushAction = false;
    private Map<String, String> params = new HashMap<>();

    public Action(JSONObject object) {
        if (object != null) {
            type = object.optInt("type", -1);
            JSONArray array = object.names();
            if (array != null && array.length() > 1) {
                for (int i = 0; i < array.length(); i++) {
                    if (!array.optString(i).equals("type")) {
                        params.put(array.optString(i), object.optString(array.optString(i)));
                    }
                }
            }
        }
    }

    //推送所需的Action
    public Action(Map<String, String> map) {
        type = Integer.parseInt(map.get("type"));
        map.remove("type");
        params = map;
        isPushAction = true;
    }

    //Uri启动Action
    public Action(String actionStr) {
        try {
            JSONObject object = new JSONObject(actionStr);
            if (object != null) {
                type = object.optInt("type", -1);
                JSONArray array = object.names();
                if (array != null && array.length() > 1) {
                    for (int i = 0; i < array.length(); i++) {
                        if (!array.optString(i).equals("type")) {
                            params.put(array.optString(i), object.optString(array.optString(i)));
                        }
                    }
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    public void jump(Context context) {
        if (type == -1) {
            return;
        }
        Intent intent = new Intent();
        switch (type) {
            case 100:
                GlobalDataManager.needSetMainTab = true;
                GlobalDataManager.save();
                intent.setClass(context, MainActivity.class);
                break;
            case 101:
                GlobalDataManager.tab = Integer.parseInt(params.get("tab"));
                GlobalDataManager.needSetTab = true;
                GlobalDataManager.needSetMainTab = true;
                GlobalDataManager.save();
                intent.setClass(context, MainActivity.class);
                break;
            case 200:
                //作品详情页
                intent.setClass(context, ContentDetailActivity.class);
                intent.putExtra("content_id", Integer.parseInt(params.get("work_id")));
                break;
            case 201:
                //优秀作品列表
                intent.setClass(context, MyAllWorksActivity.class);
                intent.putExtra(MyAllWorksActivity.EXTRAS_TYPE, MyAllWorksActivity.TYPE_GREAT_WORK);
                intent.putExtra(MyAllWorksActivity.EXTRAS_URL, params.get("url"));
                intent.putExtra(MyAllWorksActivity.EXTRAS_TITLE, params.get("title"));

                break;
            case 300:
                //个人主页
                intent.setClass(context, PersonalInfoActivity.class);
                intent.putExtra(PersonalInfoActivity.EXTRAS_ID, Integer.parseInt(params.get
                        ("user_id")));
                break;
            case 400:
                //h5
                intent.setClass(context, WebViewActivity.class);
                intent.putExtra(WebViewActivity.EXTRA_WEB_PAGE, Web.pages);
                intent.putExtra(WebViewActivity.EXTRAS_URL, params.get("url"));
                break;
            case 500:
                //类别详情
                intent.setClass(context, AuditionDetailActivity.class);
                intent.putExtra("audition_id", Integer.parseInt(params.get("cate_id")));
                intent.putExtra("movie_id", Integer.parseInt(params.get("movie_id")));
                break;
            case 600:
                //实名认证成功
                //实名认证失败
            case 601:
                intent.setClass(context, CertificationingActivity.class);
                break;
            case 700:
                //海选拉票页
                intent.setClass(context, MyAuditionGoingActivity.class);
                intent.putExtra(MyAuditionGoingActivity.EXTRAS_ID, Integer.parseInt(params.get
                        ("apply_id")));
                break;
            case 800:
                //相关消息页
                intent.setClass(context, MyNewsActivity.class);
                intent.putExtra(MyNewsActivity.EXTRAS_HAVE_NEW, true);
                break;
            case 900:
                //我的粉丝
                intent.setClass(context, FansActivity.class);
                intent.putExtra(FansActivity.EXTRAS_TYPE, FansActivity.TYPE_FANS);
                break;
            case 1000:
                //某个电影的类别选择页
                intent.setClass(context, AuditionSelectActivity.class);
                intent.putExtra(AuditionSelectActivity.EXTRA_ID, Integer.parseInt(params.get
                        ("movie_id")));
            default:
                break;
        }
        if (isPushAction) {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            if (type != 100 && type != 101) {
                intent.putExtra("start_main", true);
            }
        }
        context.startActivity(intent);

    }

}
